import cv2
import matplotlib.pyplot as plt
import numpy as np


def show(img):
    cv2.imshow("img", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

img = cv2.imread("./01_Picture/01_cat.jpg")
# 输入图 阈值 最大值 二值化类型
# cv2.threshold(src, thresh, maxvalue, type)
# 二值法 要么取maxvalue 要么取0
ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# THRESH_BINARY的反转
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
# 大于阈值部分设为阈值，否则不变
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
# 大于阈值部分不改变，否则设为0
ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
# THRESH_TOZERO的反转
ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)

titles = ['original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2,3,i+1), plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()






